<template>
  <a-modal
      :title="!dataForm.id ? '新增' : '修改'"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      okText="保存"
      cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="dataForm" :model="dataForm" :rules="validatorRules">
        <a-form-model-item label="权重id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appraiseTiesId">
          <a-input-number placeholder="请输入权重id" v-model="dataForm.appraiseTiesId" style="width: 100%" />
        </a-form-model-item>

        <a-form-model-item label="权重关系" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appraiseTies">
          <a-input placeholder="请输入权重关系" v-model="dataForm.appraiseTies" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'

export default {
  name: "weight-form",
  data () {
    return {
      visible: false,
      confirmLoading: false,
      dataForm: {
        id: '',
        appraiseTiesId: '',
        appraiseTies: '',
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      url: {
        save: '/weight/save',
        edit: '/weight/edit'
      }
    }
  },
  computed: {
    validatorRules () {
      return {
        appraiseTiesId: [
          { required: true, message: '请输入权重id!', trigger: 'change' },
        ],
        appraiseTies: [
          { required: true, message: '请输入权重关系!', trigger: 'blur' },
        ],
      }
    }
  },
  methods: {
    init(record) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
        if (record) {
          this.dataForm = Object.assign({}, record)
        }
      })
    },
    handleOk () {
      const that = this;
      // 触发表单验证
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let url, method
          if (that.dataForm.id) {
            url = this.url.edit
            method = 'put'
          } else {
            url = this.url.save
            method = 'post'
          }
          httpAction(url, that.dataForm, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              this.$emit('ok')
            } else {
              that.$message.error(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.handleCancel();
          })
        }
      })
    },
    handleCancel () {
      this.dataForm.id = ''
      this.$refs.dataForm.resetFields()
      this.visible = false
    }
  }
}
</script>

<style scoped>

</style>
